<?php

namespace EasySwoole\EasySwoole;

use EasySwoole\EasySwoole\Swoole\EventRegister;
use EasySwoole\EasySwoole\AbstractInterface\Event;
use EasySwoole\Http\Request;
use EasySwoole\Http\Response;

use EasySwoole\ORM\Db\Connection;
// use EasySwoole\ORM\DbManager;
// use EasySwoole\ORM\Db\MysqlPool;

use EasySwoole\EasySwoole\Config;

use EasySwoole\Utility\File;

use App\Pool\MysqlPool;
use App\Pool\RedisPool;
use App\Process\HotReload;


// use EasySwoole\Component\TableManager;
// use Swoole\Table;

class EasySwooleEvent implements Event
{

    public static function initialize()
    {
        date_default_timezone_set('Asia/Shanghai');
    }

    public static function mainServerCreate(EventRegister $register)
    {

        // ********* 加载自定义文件配置 ******* //
        self::loadConfig();
        // self::initSwooleTable();

        // ********* 读取配置 ******* //
        $config = Config::getInstance()->getConf();
        $poolConfig = new \EasySwoole\Pool\Config();

	   // ********  MYSQL:连接池 ******** //
        $mysqlConfig = new \EasySwoole\ORM\Db\Config($config['MYSQL']);
        \EasySwoole\ORM\DbManager::getInstance()->addConnection(new Connection($mysqlConfig), 'mysql');
        //自定义连接池
        // $mysqliPoolConfig = new \EasySwoole\Pool\Config();
        // \EasySwoole\Pool\Manager::getInstance()->register(new \App\Pool\MysqlPool($mysqliPoolConfig), 'mysql');
        
	   // ********  REDIS 连接池 ******** //
        \EasySwoole\RedisPool\Redis::getInstance()->register('redis',new \EasySwoole\Redis\Config\RedisConfig($config['REDIS']));
        //自定义连接池
    	// $redisConfig = new \EasySwoole\Redis\Config\RedisConfig($config['REDIS']);
    	// \EasySwoole\Pool\Manager::getInstance()->register(new RedisPool($poolConfig, $redisConfig),'redis');

        // **************** 监控文件变化,自动重启 **************** //
        $swooleServer = ServerManager::getInstance()->getSwooleServer();
        $swooleServer->addProcess((new HotReload('HotReload', ['disableInotify' => false]))->getProcess());

    }

    public static function onRequest(Request $request, Response $response): bool
    {
        // TODO: Implement onRequest() method.
        return true;
    }

    public static function afterRequest(Request $request, Response $response): void
    {
        // TODO: Implement afterAction() method.
    }


    /**
     * 加载自定义配置文件
     */
    public static function loadConfig()
    {
        $instance = \EasySwoole\EasySwoole\Config::getInstance();
        foreach (glob(EASYSWOOLE_ROOT.DIRECTORY_SEPARATOR.'App/Config/*.php') as $filePath){
            $instance->setConf(rtrim(basename($filePath),'.php'),require_once $filePath);
        }
    }
    /**
     * 加载内存表
     */
    public static function initSwooleTable()
    {
        //记录流对应进程号 key=stream_key
        // TableManager::getInstance()->add('stream', ['php_pid'=>['type'=>Table::TYPE_INT,'size'=>11],], 1024);
        //记录流下客户端(实现自动结束流) key=stream_key
        // TableManager::getInstance()->add('watch', ['rows'=>['type'=>Table::TYPE_STRING,'size'=>4096]], 1024);
        //记录客户端对应流(实现自动结束流) key=client_id
        // TableManager::getInstance()->add('client', ['stream_key'=>['type'=>Table::TYPE_STRING,'size'=>32]], 1024);
    }


}
